﻿using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace FanaticRock.Theme.Helpers
{
    public static class SliderHelper
    {
        #region use custom slide mode


        // 主要解决鼠标拖拽 Slider 应该也能修改时间的问题。

        public static bool GetUseCustomSliderMode(DependencyObject obj)
        {
            return (bool)obj.GetValue(UseCustomSliderModeProperty);
        }

        public static void SetUseCustomSliderMode(DependencyObject obj, bool value)
        {
            obj.SetValue(UseCustomSliderModeProperty, value);
        }

        // Using a DependencyProperty as the backing store for UseCustomSliderMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UseCustomSliderModeProperty =
            DependencyProperty.RegisterAttached("UseCustomSliderMode", typeof(bool), typeof(SliderHelper), new PropertyMetadata(false) { PropertyChangedCallback = OnCustomSliderModeChanged });

        private static void OnCustomSliderModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Slider slider)
            {
                var newValue = (bool)e.NewValue;
                if (newValue == true)
                {
                    RegisterHandler(slider);
                }
                else
                {
                    RemoveHandler(slider);
                }
            }
        }

        #region register events

        private static MouseEventHandler _onDragHandler = new MouseEventHandler((s, e) =>
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed)
            {
                if(s is Slider slider)
                {
                    slider.ReleaseMouseCapture();
                    FrameworkElementHelper.SetIsPressed(slider, false);
                }
                return;
            }
            OnClick(s, e);
        });

        private static RoutedEventHandler _onClickHandler = new RoutedEventHandler((s, e) =>
        {
            var slider = e.Source as Slider;
            if (slider == null) return;

            Mouse.Capture(slider);

            OnClick(s, e);
        });

        private static void OnClick(object sender, RoutedEventArgs e)
        {
        
            var slider = e.Source as Slider;
            if (slider == null) return;


            var pos = Mouse.GetPosition(slider);

            var leftRatio = pos.X / slider.ActualWidth;
            //var rightRatio = 1 - leftRatio;

            var value = slider.Minimum + (slider.Maximum - slider.Minimum) * leftRatio;

            slider.SetCurrentValue(Slider.ValueProperty, value);
            FrameworkElementHelper.SetIsPressed(slider, true);
            //slider.SetValue(Slider.ValueProperty, value); 经过测试下来发现都一样
        }


        private static void OnDrag(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed) return;
            OnClick(sender, e);
        }

        private static void RemoveHandler(Slider slider)
        {
            //slider.RemoveHandler(RepeatButton.ClickEvent, _onClickHandler);
            //slider.RemoveHandler(RepeatButton.MouseMoveEvent, _onDragHandler);
            slider.RemoveHandler(Slider.PreviewMouseMoveEvent, _onDragHandler);
            slider.RemoveHandler(Slider.MouseLeftButtonDownEvent, _onClickHandler);
        }

        private static void RegisterHandler(Slider slider)
        {
            RemoveHandler(slider);

            //slider.AddHandler(RepeatButton.ClickEvent, _onClickHandler);
            //slider.AddHandler(RepeatButton.MouseMoveEvent, _onDragHandler);
            slider.AddHandler(Slider.PreviewMouseMoveEvent, _onDragHandler);
            slider.AddHandler(Slider.MouseLeftButtonDownEvent, _onClickHandler);
        }

        #endregion




        #endregion


    }
}
